package View.admin;

import Controller.AdminController;
import agh.rozproszony.model.Room;
import agh.rozproszony.model.User;
import java.util.List;

public class AdminWindow extends javax.swing.JFrame {

    protected UsersTableModel usersTableModel;
    protected RoomsTableModel roomTableModel;
    protected AdminController controller;

    public AdminWindow(AdminController controller) {
        this.controller = controller;
        setLocationRelativeTo(null);
        initComponents();

        usersTableModel = new UsersTableModel();
        jTableUsers.setModel(usersTableModel);

        roomTableModel = new RoomsTableModel();
        jTableRooms.setModel(roomTableModel);
        
        
    }

    public void refreshUsersTable(List<User> usersList) {
        usersTableModel.update(usersList);

    }

    public void refreshRoomsTable(List<Room> roomsList) {
        roomTableModel.update(roomsList);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelUsers = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableUsers = new javax.swing.JTable();
        jButtonRemoveUser = new javax.swing.JButton();
        jButtonAddUser = new javax.swing.JButton();
        jPanelRooms = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableRooms = new javax.swing.JTable();
        jButtodAddRoom = new javax.swing.JButton();
        jButtomDeleteRoom = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Zarządzanie systemem");

        jTableUsers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTableUsers);

        jButtonRemoveUser.setText("Usuń użytkownika");
        jButtonRemoveUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveUserActionPerformed(evt);
            }
        });

        jButtonAddUser.setText("Dodaj użytkownika");
        jButtonAddUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddUserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelUsersLayout = new javax.swing.GroupLayout(jPanelUsers);
        jPanelUsers.setLayout(jPanelUsersLayout);
        jPanelUsersLayout.setHorizontalGroup(
            jPanelUsersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelUsersLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelUsersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE)
                    .addGroup(jPanelUsersLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButtonRemoveUser)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonAddUser)))
                .addContainerGap())
        );
        jPanelUsersLayout.setVerticalGroup(
            jPanelUsersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelUsersLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelUsersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonRemoveUser)
                    .addComponent(jButtonAddUser))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Użytkownicy systemu", jPanelUsers);

        jTableRooms.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTableRooms);

        jButtodAddRoom.setText("Dodaj pomieszczenie");
        jButtodAddRoom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtodAddRoomActionPerformed(evt);
            }
        });

        jButtomDeleteRoom.setText("Usuń pomieszczenie");
        jButtomDeleteRoom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtomDeleteRoomActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelRoomsLayout = new javax.swing.GroupLayout(jPanelRooms);
        jPanelRooms.setLayout(jPanelRoomsLayout);
        jPanelRoomsLayout.setHorizontalGroup(
            jPanelRoomsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelRoomsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelRoomsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelRoomsLayout.createSequentialGroup()
                        .addComponent(jButtomDeleteRoom)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtodAddRoom)))
                .addContainerGap())
        );
        jPanelRoomsLayout.setVerticalGroup(
            jPanelRoomsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelRoomsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelRoomsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtodAddRoom)
                    .addComponent(jButtomDeleteRoom))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Pomieszczenia", jPanelRooms);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonRemoveUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveUserActionPerformed
        for (int row : jTableUsers.getSelectedRows()) {
            User userToDelete = usersTableModel.getUserAtRow(row);
            controller.deleteUser(userToDelete);
        }
        controller.refreshUsersInGUI();
    }//GEN-LAST:event_jButtonRemoveUserActionPerformed

    private void jButtonAddUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddUserActionPerformed
        controller.openAddUserDialog();
    }//GEN-LAST:event_jButtonAddUserActionPerformed

    private void jButtomDeleteRoomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtomDeleteRoomActionPerformed
        for (int row : jTableRooms.getSelectedRows()) {
            Room roomToDelete = roomTableModel.getRoomAtRow(row);
            controller.deleteRoom(roomToDelete);
        }
        controller.refreshRoomsInGUI();
    }//GEN-LAST:event_jButtomDeleteRoomActionPerformed

    private void jButtodAddRoomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtodAddRoomActionPerformed
        controller.openRoomAddDialog();
    }//GEN-LAST:event_jButtodAddRoomActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtodAddRoom;
    private javax.swing.JButton jButtomDeleteRoom;
    private javax.swing.JButton jButtonAddUser;
    private javax.swing.JButton jButtonRemoveUser;
    private javax.swing.JPanel jPanelRooms;
    private javax.swing.JPanel jPanelUsers;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTableRooms;
    private javax.swing.JTable jTableUsers;
    // End of variables declaration//GEN-END:variables
}
